<template>
	<view class="content">
		<button type="default" @click="subscribe">订阅信息</button>
		<button type="default" @click="getRunNum">获取微信步数</button>
		<button type="default" @click="getWeather">获取天气预报</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: 'Hello',
				tmplIds: ['HaUu4SPJ3W8yX88V6kcU4rWVHOP8dJsqkvEm9y3R1Ng', 'dI4eWe1Hx0y8cLzw0MZcNvYRIOK2mP1c3dhXmslFe6U']
			}
		},
		onLoad() {
			this.login()
			// this.getSubscribeMeaage()
		},
		methods: {
			// test(){
			// 	uniCloud.callFunction({
			// 		name:'baseCommon',
			// 		data:{
			// 			action:'register',
			// 			params:{
			// 				username:'lcp',
			// 				password:'123456'
			// 			}
			// 		},
			// 		success: (res) => {
			// 			uni.setStorageSync('uni_id_token',res.result.token)
			// 			uni.setStorageSync('uni_id_token_expired',res.result.tokenExpired)
			// 		}
			// 	})
			// },
			getWeather(){
				uniCloud.callFunction({
					name:'chb-weather',
					data:{},
					success: (res) => {
						console.log(res,'res')
					}
				})
			},
			subscribe() {
				uni.requestSubscribeMessage({
					tmplIds: [this.tmplIds[1]],
					success: res => {
						if (res[this.tmplIds[1]] == 'accept') {
							this.getSubscribeMeaage(this.tmplIds[1])
						} else {
							//xxxx
						}
					}
				})
			},
			getSubscribeMeaage(templateId) {
				uniCloud.callFunction({
					name: 'subscribeMessage',
					data: {
						token: uni.getStorageSync('token'),
						templateId
					},
					success: res => {
						console.log(res, 'res')
					}
				})
			},
			aginAuth() {
				uni.getSetting({
					success: (res) => {
						if (!res.authSetting['scope.werun']) {
							uni.openSetting({
								success: (res) => {
									this.getRunNum()
								},
								fail: (err) => {
									console.log(err, 'err11')
								}
							})
						}
					}
				})
			},
			getRunNum() {
				uni.getWeRunData({
					success: res => {
						const {
							encryptedData,
							iv
						} = res
						uniCloud.callFunction({
							name: 'wxRunData',
							data: {
								sessionKey: uni.getStorageSync('session_key'),
								encryptedData,
								iv
							},
							success: res => {
								console.log(res, 'wxevent');
							}
						})
					},
					fail: () => {
						uni.showModal({
							title: '提示', //提示的标题,
							content: '微信运动授权', //提示的内容,
							showCancel: true, //是否显示取消按钮,
							cancelText: '取消', //取消按钮的文字，默认为取消，最多 4 个字符,
							cancelColor: '#000000', //取消按钮的文字颜色,
							confirmText: '确定', //确定按钮的文字，默认为取消，最多 4 个字符,
							confirmColor: '#3CC51F', //确定按钮的文字颜色,
							success: res => {
								if (res.confirm) {
									this.aginAuth();
								} else if (res.cancel) {
									console.log('用户点击取消')
									uni.showToast({
										title: '需要微信运动授权限',
										icon: 'none',
										duration: 2000,
									})
								}
							}
						});
					}
				})
			},
			login() {
				uni.login({
					provider: 'weixin',
					success: (res) => {
						uniCloud.callFunction({
							name: 'login',
							data: {
								code: res.code
							},
							success: (result) => {
								uni.setStorageSync('token', result.result.token)
								uni.setStorageSync('session_key', result.result.session_key)
							}
						})
					}
				})
			}
		},

	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>
